package com.cucn.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cucn.commonutils.Result;
import com.cucn.eduservice.entity.EduTeacher;
import com.cucn.eduservice.entity.vo.TeacherQuery;
import com.cucn.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author KouChaoJie
 * @since 2021-09-24
 */
@Api(value = "讲师Controller", tags = "讲师管理")
@RestController
//@CrossOrigin
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;

    /**
     * 查询讲师表中所有数据
     * rest风格url操作
     *
     * @return 所有讲师信息
     */
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/findAll")
    public Result findAllTeacher() {
        //调用service方法实现查询所有讲师数据
        List<EduTeacher> eduTeachers = eduTeacherService.list(null);
        return Result.ok().data("items", eduTeachers);
    }

    /**
     * 通过id逻辑删除讲师
     *
     * @param id 需要删除的讲师id
     * @return 是否删除成功
     */
    @ApiOperation(value = "根据ID逻辑删除讲师")
    @DeleteMapping("/{id}")
    public Result removeTeacher(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable("id") String id) {
        boolean removeById = eduTeacherService.removeById(id);
        if (removeById) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    /**
     * 分页查询讲师的方法
     *
     * @param current 当前页
     * @param limit   每页记录数
     * @return 分页Result信息
     */
    @ApiOperation(value = "分页查询讲师")
    @GetMapping("/pageTeacher/{current}/{limit}")
    public Result pageListTeacher(@PathVariable("current") Long current,
                                  @PathVariable("limit") Long limit) {
        //创建page对象
        Page<EduTeacher> teacherPage = new Page<>(current, limit);
        //调用方法实现分页
        //调用方法时，底层封装，把分页所有数据都封装到了Page对象中
        eduTeacherService.page(teacherPage, null);

        //总记录数
        long total = teacherPage.getTotal();
        //每页数据集合
        List<EduTeacher> records = teacherPage.getRecords();

        return Result.ok().data("total", total).data("rows", records);
    }

    /**
     * 条件查询带分页方法
     *
     * @param current      当前页
     * @param limit        每页记录数
     * @param teacherQuery teacher查询条件对象
     * @return 分页Result信息
     */
    @PostMapping("/pageTeacherCondition/{current}/{limit}")
    public Result pageTeacherCondition(@PathVariable("current") Long current,
                                       @PathVariable("limit") Long limit,
                                       @RequestBody(required = false) TeacherQuery teacherQuery) {
        Page<EduTeacher> teacherPage = new Page<>(current, limit);

        //构造条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //多条件组合查询
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //判断条件值是否为空，如果不为空拼接条件
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            wrapper.like("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create", end);
        }

        //排序
        wrapper.orderByDesc("gmt_create");

        eduTeacherService.page(teacherPage, wrapper);

        //总记录数
        long total = teacherPage.getTotal();
        //每页数据集合
        List<EduTeacher> records = teacherPage.getRecords();

        return Result.ok().data("total", total).data("rows", records);
    }

    /**
     * 添加讲师
     *
     * @param eduTeacher 添加的讲师信息
     * @return Result信息
     */
    @PostMapping("/addTeacher")
    public Result addTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean save = eduTeacherService.save(eduTeacher);
        if (save) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    /**
     * 根据讲师id进行查询
     */
    @ApiOperation(value = "根据讲师id进行查询")
    @GetMapping("/getTeacher/{id}")
    public Result getTeacher(@PathVariable("id") String id) {
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return Result.ok().data("teacher", eduTeacher);
    }

    /**
     * 讲师修改
     *
     * @param eduTeacher 修改后的讲师信息
     * @return Result信息
     */
    @ApiOperation(value = "根据讲师id进行修改")
    @PostMapping("/updateTeacher")
    public Result updateTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean updateById = eduTeacherService.updateById(eduTeacher);
        if (updateById) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }
}

